const fs = require('fs');
const path = require('path');

module.exports = function (pagePath, name) {
  const apiPath = pagePath + '/API';
  if (!fs.existsSync(apiPath)) {
    fs.mkdirSync(apiPath);
  }
  const apiName = `${name}API`;

  const apiFile = apiPath + `/${apiName}.js`;
  if (!fs.existsSync(apiFile)) {
    const template = [];
    template.push(`import { createDeleteAPI, createGetAPI, createPostJsonAPI, createPutJsonAPI  } from '../../../shared/utils/createAPI';`);
    template.push(``);
    template.push(`export default {`);
    template.push(``);
    template.push(`};`);
    template.push(``);
    fs.writeFileSync(apiFile, template.join('\n'));
  }

  const indexPath = apiPath + '/index.js';
  if (!fs.existsSync(indexPath)) {
    const template = [];
    template.push(`export { default as ${apiName} } from './${apiName}';`);
    template.push(``);
    fs.writeFileSync(indexPath, template.join('\n'));
  }
}
